<?php

namespace app\index\controller;

use app\common\model\user\Users;
use think\Controller;
use think\Db;
use think\Request;
use app\common\controller\Curls;

class Login extends Controller
{
	public function test()
	{
		//
		$res = Curls::curl('https://api.huobi.pro/market/tickers');
		dump($res);
	}
    /**
     * 注册页面
     */
    public function reg(){
        return view();
    }
    /**
     * 用户注册
     */
    public function reg_do(Request $request){
        $data = $request->post('');
        //推荐人在不在
        $tjuser=Db::name('users')->where(['no'=>$data['tjuser']])->value('id');
        if(!$tjuser){
            return jsonError('推荐人不存在');
        }
        //一个手机号一个账号
        $mobile=Db::name('users')->where(['user_name'=>$data['mobile']])->value('id');
        if($mobile){
            return jsonError('该手机号已被注册');
        }
        //创建员工编号
        $no=$this->user_no();
        $insert=[
            'no'=>$no,
            'user_name'=>$data['mobile'],
            'tjuser'=>$data['tjuser'],
            'password'=>md5($data['password']),
            'pay_pwd'=>md5($data['pay_pwd']),
            'mobile'=>$data['mobile'],
            'create_time'=>time(),
            'update_time'=>time(),
        		'act_id' => $this->findAct($tjuser)
        ];
        $res=Db::name('users')->insert($insert);
        if($res){
            return jsonSuccess('注册成功');
        }else{
            return jsonError('注册失败,请检查网络状态');
        }
    }
    
    /**
     * 寻找代理
     * @param unknown $user_id
     * @return unknown
     */
    public function findAct($user_id)
    {
    	$user = Users::get($user_id);
    	if($user->is_act) {
    		return $user_id;
    	}else{
    		$tjuser_id = Users::where(['no' => $user->tjuser])->value('id');
    		return $this->findAct($tjuser_id);
    	}
    }
    /**
     * 员工编号创建
     */
    public function user_no(){
        $count = Users::count();
        $no = 'no'.(10000 + $count);
        return $no;
    }
    /**
     * 用户登录
     */
    public function login_do(Request $request)
    {
        $data=$request->post('');
//        $data=[
//            'user_name'=>17660460001,
//            'password'=>2,
//        ];
        $user=Db::name('users')->where(['user_name'=>$data['user_name']])->find();
        if(!$user){
            return jsonError('账号不存在');
        }
        if($user['state']==2){
            return jsonError('该账号已被封号');
        }
        if($user['state']==0){
            return jsonError('账号不存在');
        }
        if(md5($data['password'])!==$user['password']){
            return jsonError('登录密码错误');
        }
        session('user_id',$user['id']);
        return jsonSuccess([
        		'id' => $user['id'],
        		'hand_lock' => $user['hand_lock']
        ]);
    }
    
    /**
     * 设置手势登录
     * @param Request $request
     * @return unknown
     */
    public function setLock(Request $request)
    {
    	$data = $request->param('');
    	$user = Users::get($data['id']);
    	if(!$user->hand_lock){
    		$user->save(['hand_lock' => $data['hand_lock']]);
    		return jsonSuccess('手势设置成功');
    	}else{
    		return $user->hand_lock == $data['hand_lock'] ? jsonSuccess('验证成功') : jsonError('验证失败');
    	}
    	
    }
    
    /**
     * 退出登录
     * @return unknown
     */
    public function outLogin()
    {
    	session('user_id',null);
    	return jsonSuccess('退出成功');
    }

}